/**
 * 
 */
package com.dycn.posdashboard.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.dycn.posdashboard.data.MerchantInfo;
import com.dycn.posdashboard.dbhelper.DbContextHolder;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

/**
 * @author gongqiyi
 *
 */
public class BaseDaoImpl extends SqlMapClientDaoSupport implements IBaseDao {
    protected final Log log = LogFactory.getLog(getClass());

	
	@Override
	public Integer systemInit(String dbSource) {
		// TODO Auto-generated method stub
        DbContextHolder.setDbSource(dbSource);

        log.debug("initializeConnectPool : dbType=" + dbSource);
        this.getSqlMapClientTemplate().queryForObject("initializeConnectPool");
        return 1;
	}
	/**
	 * 
	 * @param statementName
	 * @param list
	 * @throws Exception 
	 */
	public void batchUpdate(final String statementName, final List list) throws Exception {
	       try {
	           if (list != null) {
	              this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
	            	  @Override
	                  public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
	                     executor.startBatch();
	                     for (int i = 0, n = list.size(); i < n; i++) {
	                         executor.update(statementName, list.get(i));
	                     }
	                     executor.executeBatch();
	                     return null;
	                  }
	              });

	           }

	       } catch (Exception e) {
	           if (log.isDebugEnabled()) {
	              e.printStackTrace();
	              log.debug("batchUpdate error: id [" + statementName + "], parameterObject ["+ list + "].  Cause: "+ e.getMessage());
	           }
	           
	           throw e;
	       }
	    }

	/**
	 * 
	 * @param statementName
	 * @param list
	 * @throws Exception 
	 */
	    public void batchInsert(final String statementName, final List list) throws Exception {
	       try {
	           if (list != null) {
	              this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
	            	  @Override
	                  public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
	                     executor.startBatch();
	                     for (int i = 0, n = list.size(); i < n; i++) {
	                    	 try{
	                    		 executor.insert(statementName, list.get(i));
	                         }catch(SQLException exp){
	                        	 System.out.println("batchInsert："+exp.getMessage());
	                        	 
	                        	 if(exp instanceof MySQLIntegrityConstraintViolationException){
	                        		 
	                        	 }
	                        	 //com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
	                        	 if(exp.getMessage().indexOf("Duplicate entry")!=-1){
	                        		//continue;
	                        	 }else{
	                        		 throw exp;
	                        	 }
	                         }
	                     }
	                     executor.executeBatch();
	                     return null;
	                  }
	              });
	           }

	       } catch (Exception e) {

	           if (log.isDebugEnabled()) {
	              e.printStackTrace();
	              log.debug("batchInsert error: id [" + statementName + "], parameterObject ["+ list + "].  Cause: "+ e.getMessage());
	           }
	           
	           throw e;
	       }
	    }

	    /**
	     * 
	     * @param statementName
	     * @param list
	     * @throws Exception 
	     */
	    public void batchDelete(final String statementName, final List list) throws Exception {
	       try {
	           if (list != null) {
	              this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
	            	  @Override
	                  public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
	                     executor.startBatch();
	                     for (int i = 0, n = list.size(); i < n; i++) {
	                         executor.delete(statementName, list.get(i));
	                     }
	                     executor.executeBatch();
	                     return null;
	                  }
	              });
	           }

	       } catch (Exception e) {
	           if (log.isDebugEnabled()) {
	              e.printStackTrace();
	              log.debug("batchDelete error: id [" + statementName + "], parameterObject ["+ list + "].  Cause: "+ e.getMessage());
	           }
	           
	           throw e;
	       }
	    }

		@Override
		public List<MerchantInfo> getBranch2MerchantMapping(
				String dbSource) {
			// TODO Auto-generated method stub
	        DbContextHolder.setDbSource(dbSource);
	        log.debug("getBranch2MerchantMapping dbType=" + dbSource);
	        return (List<MerchantInfo>) this.getSqlMapClientTemplate().queryForList("findBranch2MerchantMapping");
		}



}
